package jobteam.contacteventstocalendar;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class hasNoEventContactsList extends Activity {
	private String TAG = "hasNoEventContactsList";

	private ArrayList<Integer> aEventRawId;
	private ArrayList<Integer> aRawId;
	private ArrayList<String> aName;
	private ArrayList<ContactIDandName> mIDandNAME;

	private int selectId;

	private Context m_Context;
	private Cursor dataCursor;

	private Uri DataBaseUri = ContactsContract.Data.CONTENT_URI;

	private static final String[] CONTACTS_DATA_PROJECTION = new String[]{
		ContactsContract.Data.MIMETYPE,			// 0
		ContactsContract.Data.RAW_CONTACT_ID,	// 1
		ContactsContract.Data.DATA1,			// 2
	};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listviewer);
		
		Log.d(TAG, "test1");
		m_Context = this;

		aName = new ArrayList<String>();
		aRawId = new ArrayList<Integer>();
		aEventRawId = new ArrayList<Integer>();
		mIDandNAME = new ArrayList<ContactIDandName>();


	}

	@Override
	protected void onResume() {
		super.onResume();
		aName.clear();
		aRawId.clear();
		aEventRawId.clear();
		mIDandNAME.clear();

		aEventRawId = rawIdDatas();		
		contactDatas();
		if(mIDandNAME.size() <= 0){
			setContentView(R.layout.main);
			new AlertDialog.Builder(this)
			.setTitle("Information")
			.setMessage(getResources().getString(R.string.has_no_Contact_no_Event))
			.setIcon(R.drawable.icon)
			.setCancelable(false)
			.setPositiveButton(R.string.Confirm, new DialogInterface.OnClickListener(){
				public void onClick(DialogInterface dialog, int whichButton){

				}
			}).show();
			return;
		}
		
		for(int i = 0 ; i < mIDandNAME.size() ; i++){
			aName.add(mIDandNAME.get(i).ContactName);
			aRawId.add(mIDandNAME.get(i).ContactID);
		}

		ArrayAdapter<String> aAdapter;
		aAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, aName);
		ListView lv = (ListView) findViewById(R.id.listviewer);


		lv.setAdapter(aAdapter);

		lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				selectId = aRawId.get(position);

				Intent intent = new Intent("android.intent.action.INSERT_EVENT").setClass(hasNoEventContactsList.this, addContactEventActivity.class);
				intent.putExtra("SelectId", selectId);
				startActivity(intent);
			}
		});
		Log.d(TAG, "test2");
		Log.d(TAG, "test3");
	}

	public Cursor ContactCursor(){
		Uri DataBaseUri = ContactsContract.Data.CONTENT_URI;
		Cursor cusor = m_Context.getContentResolver().query(DataBaseUri, 
				CONTACTS_DATA_PROJECTION, 
				DataTest() , null, "data1 ASC, raw_contact_id ASC");

		return cusor;
	}

	public String DataTest(){
		if(aEventRawId.size() > 0){

			String test = ContactsContract.Data.RAW_CONTACT_ID + " not like " + aEventRawId.get(0);
			for(int i=1 ; i<aEventRawId.size(); i++){
				test += " AND " + ContactsContract.Data.RAW_CONTACT_ID + " not like " + aEventRawId.get(i);
			}
			return test;
		}    
		return null;
	}
	///////////////////////////////////////////////////////////////////////////////
	public void contactDatas(){

		dataCursor = ContactCursor();

		if(dataCursor.moveToFirst()) {
			for (int i = 0; i < dataCursor.getCount(); i++) {
				String str = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.MIMETYPE));
				if(str.equals(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)) {
					int rawContactID = dataCursor.getInt(dataCursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
					String ContactName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DATA1));
					if(ContactName != null){
						ContactIDandName tmpIDNAME = new ContactIDandName(rawContactID, ContactName);
						mIDandNAME.add(tmpIDNAME);
					}
				}
				dataCursor.moveToNext();
			}	
		}
	}


	///////////////////////////////////////////////////////////////////////////////

	public ArrayList<Integer> rawIdDatas(){

		ArrayList<Integer> contactRawIntArray = new ArrayList<Integer>();

		dataCursor = m_Context.getContentResolver().query(DataBaseUri, 
				CONTACTS_DATA_PROJECTION, 
				ContactsContract.Data.MIMETYPE + " like " + "'" + "vnd.android.cursor.item/contact_event"
				+ "'"  , null, null);

		if(dataCursor.moveToFirst()) {
			for (int i = 0; i < dataCursor.getCount(); i++) {
				int rawInt = dataCursor.getInt(dataCursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
				contactRawIntArray.add(rawInt);
				dataCursor.moveToNext();
			}	
		}
		return contactRawIntArray;
	}
	///////////////////////////////////////DATA ///////////////////////////////////
}